package net.joke.base.systems;

import net.joke.base.components.Expires;
import net.joke.base.components.Position;
import net.joke.base.components.Renderable;

import com.artemis.ComponentMapper;
import com.artemis.Entity;
import com.artemis.EntityProcessingSystem;

public class ExpirationSystem  extends EntityProcessingSystem {

	private ComponentMapper<Expires> expiresMapper;
	private ComponentMapper<Position> positionMapper;
	private ComponentMapper<Renderable> renderableMapper;

	public ExpirationSystem() {
		super(Expires.class);
	}

	@Override
	public void initialize() {
		expiresMapper = new ComponentMapper<Expires>(Expires.class, world);
		positionMapper = new ComponentMapper<Position>(Position.class, world);
		renderableMapper = new ComponentMapper<Renderable>(Renderable.class, world);
	}

	@Override
	protected void process(Entity e) {
		Expires expires = expiresMapper.get(e);
		expires.reduceLifeTime(world.getDelta());

		if (expires.isExpired()) {
//			Renderable r = renderableMapper.get(e);
//			if (r != null) {
//				r.dispose();
//			}
			world.deleteEntity(e);
		}

	}

}
